package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.service.RoleService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author lixiyong01
 * @date 2022.12.19
 * 用户权限鉴权
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/user_permission")
public class UserPermissionController {

    @Autowired
    private RoleService roleService;

    @Data
    private static class RequestDefine {
        public String permissionUrl;
    }

    @RestGrpcMapping("/validate.do")
    public NTSTATUS validate(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            String userName = params.userID;
            RequestDefine aa = params.driverObject(RequestDefine.class);
            String permissionUrl = aa.permissionUrl;
            if (StringUtils.isEmpty(permissionUrl)) {
                result.setData(true);
                return NTSTATUS.SUCCESS_STATUS();
            }
            result.setData(roleService.validate(userName, permissionUrl));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
}
